package com.winit.openapi.gt.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.spi.context.CommandContext;
import com.winit.oms.spi.gt.FbaLableDataFileService;
import com.winit.oms.spi.gt.command.FbaLableDataFileCommand;
import com.winit.oms.spi.gt.vo.FbaLableDataFileVo;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.ValidateUtil;

/**
 * fba标签上传
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * youming.fan 	1.0  		2017-11-13 	Created
 * 
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping(value = "/fbaLableDataFile")
public class FbaLableDataFileController extends BaseController {

	@Resource
	private FbaLableDataFileService fbaLableDataFileService;

	/**
	 * 新增
	 * 
	 * @param requestMsg
	 * @param responseMsg
	 * @return
	 */
	@RequestMapping(value = "/addFbaLableDataFile")
	public String addFbaLableDataFile(
			@ModelAttribute RequestMsg requestMsg,
			@ModelAttribute ResponseMsg responseMsg) {
		FbaLableDataFileCommand command = new FbaLableDataFileCommand();
		Object obj = requestMsg.getData();
		if (obj instanceof JSONObject) {
			JSONObject json = (JSONObject) obj;
			ValidateUtil.validInteger(json, "orderId");
			ValidateUtil.validNotNull(json, "fileUrl");
			ValidateUtil.validNotNull(json, "fileName");
			command.setOrderId(json.getLong("orderId"));
			command.setFileUrl(json.getString("fileUrl"));
			command.setFileName(json.getString("fileName"));
			command.setCtx(CommandContext.getContext());
			FbaLableDataFileVo vo = fbaLableDataFileService
					.addFbaLableDataFile(command);
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("fbaLableDataFileVo", buildFbaLableDataFileMap(vo));
			responseMsg.setData(data);
		}
		return SUCCESS;
	}

	@RequestMapping(value = "/deleteFileById")
	public String deleteFbaLableDateFileById(
			@ModelAttribute RequestMsg requestMsg,
			@ModelAttribute ResponseMsg responseMsg) {
		FbaLableDataFileCommand command = new FbaLableDataFileCommand();
		Object obj = requestMsg.getData();
		if (obj instanceof JSONObject) {
			JSONObject json = (JSONObject) obj;
			ValidateUtil.validNotNull(json, "orderId");
			ValidateUtil.validNotNull(json, "id");
			
			if (json.containsKey("id")) {
				command.setId(json.getLong("id"));
			}
			command.setOrderId(json.getLong("orderId"));
			command.setCtx(CommandContext.getContext());
			Long orderId = fbaLableDataFileService
					.deleteFbaLableDateFileById(command);
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("orderId", orderId);
			responseMsg.setData(data);
		}
		return SUCCESS;
	}

	@RequestMapping(value = "/queryByOrderId")
	public String queryByOrderId(@ModelAttribute RequestMsg requestMsg,
			@ModelAttribute ResponseMsg responseMsg) {
		FbaLableDataFileCommand command = new FbaLableDataFileCommand();
		Object obj = requestMsg.getData();
		if (obj instanceof JSONObject) {
			JSONObject json = (JSONObject) obj;
			ValidateUtil.validNotNull(json, "orderId");
			command.setOrderId(json.getLong("orderId"));
			command.setCtx(CommandContext.getContext());
			List<FbaLableDataFileVo> list = fbaLableDataFileService
					.queryByOrderId(command);
			List<Map<String, Object>> rslist = new ArrayList<Map<String, Object>>();
			Map<String, Object> map = null;
			for (FbaLableDataFileVo vo : list) {
				map = buildFbaLableDataFileMap(vo);
				rslist.add(map);
			}
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("list", rslist);
			responseMsg.setData(data);
		}
		return SUCCESS;
	}
	
	@RequestMapping(value = "/queryTransportCodeByProductCode")
	public String queryTransportTypeByProductCode(@ModelAttribute RequestMsg requestMsg,
			@ModelAttribute ResponseMsg responseMsg){
		FbaLableDataFileCommand command = new FbaLableDataFileCommand();
		Object obj = requestMsg.getData();
		if (obj instanceof JSONObject) {
			JSONObject json = (JSONObject) obj;
			ValidateUtil.validNotNull(json, "productCode");
			command.setProductCode(json.getString("productCode"));
			command.setCtx(CommandContext.getContext());
			String transportCode= fbaLableDataFileService
					.queryTransportTypeByProductCode(command);
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("transportCode", transportCode);
			responseMsg.setData(data);
		}
		return SUCCESS;
	}

	public Map<String, Object> buildFbaLableDataFileMap(FbaLableDataFileVo vo) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", vo.getId());
		map.put("orderId", vo.getOrderId());
		map.put("fileUrl", vo.getFileUrl());
		map.put("fileName", vo.getFileName());
		map.put("createdby", vo.getCreatedby());
		map.put("created", vo.getCreated());
		map.put("updatedby", vo.getUpdatedby());
		map.put("updated", vo.getUpdated());
		map.put("organizationId", vo.getOrganizationId());
		map.put("isActive", vo.getIsActive());
		map.put("isDelete", vo.getIsDelete());
		return map;
	}
}
